package com.jash.labor.context;

import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


public class Result<T> implements Serializable {
    private static Logger LOGGER = LoggerFactory.getLogger(Result.class);
    public static final Result<Object> SUCCESS = new Result<>(true);
    public static final Result<Object> FAIL = new Result<>(false);

    /**
     * 错误编码
     */
    private int code;

    /**
     * 本地化的信息,对应错误编码的本地化说明
     */
    private String msg;

    /**
     * 返回值对象
     */
    private T data;

    public Result() {
    }

    public static Result resultMessage(String msg) {
        Result result = new Result();
        result.setCode(1);
        result.setMsg(msg);
        return result;
    }

    /**
     * 构建分页返回数据
     * @param pageNo
     * @param pageSize
     * @param total
     * @param data
     * @return
     */
    public static Result buildPage(Integer pageNo, Integer pageSize, Integer total, Object data) {
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("pageNo", pageNo);
        pageMap.put("pageSize", pageSize);
        pageMap.put("total", total);
        pageMap.put("data", data);

        Result result = new Result();
        result.setData(pageMap);
        return result;
    }

    /**
     * 构建分页返回数据
     * @param pageNo
     * @param pageSize
     * @param total
     * @return
     */
    public static Result buildPage(Integer pageNo, Integer pageSize, Integer total) {
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("pageNo", pageNo);
        pageMap.put("pageSize", pageSize);
        pageMap.put("total", total);

        Result result = new Result();
        result.setData(pageMap);
        return result;
    }

    public Result(String fileName) {
        if (StringUtils.isNotEmpty(fileName)) {
            this.msg = fileName;
            this.code = 0;
            this.data = (T) Collections.emptyMap();
        } else {
            this.msg = "上传失败";
            this.code = 1;
            this.data = (T) Collections.emptyMap();
        }
        LOGGER.info(this.toString());
    }

    public Result(ErrorResult resultCode) {
        if (resultCode != null) {
            this.msg = resultCode.getError();
            this.code = resultCode.getErrorCode();
            this.data = (T) Collections.emptyMap();
        } else {
            this.msg = "系统故障，请联系管理员";
            this.code = 99999;
            this.data = (T) Collections.emptyMap();
        }
        LOGGER.info(this.toString());
    }

    public Result(boolean status) {
        if (status) {
            this.msg = "success";
            this.code = 0;
            this.data = (T) Collections.emptyMap();
        } else {
            this.msg = "fail";
            this.code = 1;
            this.data = (T) Collections.emptyMap();
        }
        LOGGER.info(this.toString());
    }
    public Result(boolean status,String msg) {
        if (status) {
            this.msg = "success";
            this.code = 0;
            this.data = (T) Collections.emptyMap();
        } else {
            this.msg = msg;
            this.code = 1;
            this.data = (T) Collections.emptyMap();
        }
        LOGGER.info(this.toString());
    }
    public Result(T data) {
        this.data = data;
        this.msg = "success";
        this.code = 0;
        LOGGER.info(this.toString());
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }
}
